package com.iterator.service.web.marketManager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.wy.issue.Issue;
import com.iterator.bean.web.wy.issue.IssueExample;
import com.iterator.bean.web.wy.create.CreateBO;
import com.iterator.bean.web.wy.issue.IssueVO;
import com.iterator.mapper.web.marketManager.IssueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Silence_W
 * @date: 2021-01-10 12:04
 **/
@Service
public class IssueServiceImpl implements  IssueService {

    @Autowired
    IssueMapper issueMapper;

    //问答数据显示
    @Override
    public IssueVO queryIssue(Integer page, Integer limit,String question, String sort, String order) {

        PageHelper.startPage(page,limit);
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort+" "+order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if (question!=null&&question.length()!=0){
            criteria.andQuestionLike("%"+question+"%");
        }
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        int total = (int) issuePageInfo.getTotal();
        IssueVO issueVO = new IssueVO();
        issueVO.setItems(issues);
        issueVO.setTotal(total);


        return issueVO;
    }

    //新增问答数据
    @Override
    public Issue insertIssue(CreateBO createBO) {

        int insert = issueMapper.insert(new Issue(null, createBO.getQuestion(), createBO.getAnswer(), new Date(), new Date(), false));
        int i = issueMapper.selectMaxId();
        Issue issue = issueMapper.selectByPrimaryKey(i);
        return issue;
    }

    //更新问答数据
    @Override
    public Issue update(Issue issue) {


        int i = issueMapper.updateByPrimaryKey(issue);
        Issue issue1 = issueMapper.selectByPrimaryKey(issue.getId());
        return issue1;
    }

    //删除问答数据
    @Override
    public void delete(Issue issue) {
        issueMapper.deleteByPrimaryKey(issue.getId());

    }
}
